/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;
import beans.ProductBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class ProductDAO extends AbstractDAO<ProductBean>{

    @Override
    public void insert(ProductBean product) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "INSERT INTO PRODUCT(productName,productDescription, productBarCode)values('"
                + product.getProductName()+"','"
                + product.getProductDescription()+"','"
                + product.getProductBarCode()+ "');";
            dataBaseManager.executeUpdate(sqlStatement);
            } catch (DataBaseException exception) {
               throw new DAOException(exception.getExceptionMessage(),exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
       
    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "DELETE FROM product " + condition;
                dataBaseManager.executeUpdate(sqlStatement);
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
    }

    @Override
    public void update(ProductBean product, String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "UPDATE product SET productId='"+product.getProductId()+
                         "',productName='"+ product.getProductName()+
                         "',productDescription='"+ product.getProductDescription()+
                         "',productBarCode='"+ product.getProductBarCode()+
                         "' "+condition;      
                dataBaseManager.executeUpdate(sqlStatement);
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
    }

    @Override
    protected ArrayList<ProductBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM PRODUCT "+condition;
            ArrayList<ProductBean> productBeanList = null;
            productBeanList = createProductBeanList(dataBaseManager.executeQuery(sqlStatement));
            return productBeanList;
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getMessage(), exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
        
    }


    @Override
    protected ProductBean search(String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "SELECT * FROM PRODUCT ";
                ProductBean productBean = null;
                productBean = createProductBean(dataBaseManager.executeQuery(sqlStatement));
                return productBean;
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getMessage(), exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
        
    }
    

 private ArrayList<ProductBean> createProductBeanList(ResultSet queryResult) throws DAOException{
        try {
            ArrayList<ProductBean> productBeansList = new ArrayList();
            while (queryResult.next()) {
                    productBeansList.add((ProductBean) beansGenerator.generateBean(queryResult, ProductBean.class));
            }
            return productBeansList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(), exception);
        } catch (ORMException exception) {
            throw new DAOException(exception.getMessage(), exception);
        }
    }

    private ProductBean createProductBean(ResultSet queryResult) throws DAOException{
        try {
            moveQueryResultToBegin(queryResult);
            return (ProductBean) beansGenerator.generateBean(queryResult, ProductBean.class);
        } catch (ORMException exception) {
            throw new DAOException(exception.getMessage(), exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(), exception);
        }
    }


    
     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }

     public ProductBean searchProduct(int productId) throws DAOException{
            return search("WHERE productId='" + productId + "';");
     }

     public void deleteProduct(int productId) throws DAOException{
            delete("WHERE productId='"+ productId+"'");
     }

     public void updateProduct(ProductBean product) throws DAOException{
            update(product,"WHERE productId='"+ product.getProductId()+"'");
     }


}
